﻿using DDD.Application;
using DDD.EFCore;
using GoodsModule.Domain.GoodsAggregate;
using GoodsModule.Infrastructure.Repositories;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoodsModule.Application.Commands
{
    public class DeleteGoodsCategoryCommannd : ICommand
    {
        public int Id { get; set; }
    }

    public class DeleteGoodsCategoryCommanndHandler(ILogger<DeleteGoodsCategoryCommanndHandler> logger, GoodsCategoryRepository repository)
        : ICommandHandler<DeleteGoodsCategoryCommannd>
    {
        public async Task Handle(DeleteGoodsCategoryCommannd request, CancellationToken cancellationToken)
        {
            var goodsCategory = await repository.GetAsync(request.Id);
            if (goodsCategory == null)
            {
                throw new InvalidOperationException($"找不到id为{request.Id}的商品分类");
            }
            goodsCategory.Delete();
            await repository.UpdateAsync(goodsCategory);
            logger.LogInformation("DeleteGoodsCategoryCommannd GoodsCategory {}", request.Id);
        }
    }
}
